//
//  HWGameClassificationViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/9/27.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWGameClassificationViewController.h"
#import "HWSearchController.h"
#import "HWFenleishitTuViewController.h"
#import "HWRenYoubangViewController.h"
#import "HWxingyoubangViewController.h"
#import "HWyuyueBanViewController.h"
#import "HWQCKaiFViewController.h"  // 开服
#import "SDCycleScrollViewCdb.h"




@interface HWGameClassificationViewController ()<UITextFieldDelegate,UIScrollViewDelegate,SDCycleScrollViewCdbDelegate>{
    UIImageView *navBarHairlineImageView;
    
    
    
}


@property(strong,nonatomic) SDCycleScrollViewCdb *cycleScrollView;
@property (nonatomic, strong) NSMutableArray *titleButtons;
@property (nonatomic,strong)  NSMutableArray *biaotiArray;
@property (nonatomic, weak) UIButton *selectButton;
@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (nonatomic, assign) BOOL isInitialize;
@property (strong,nonatomic) UIImageView  *xiantiaoimageview;
@property(weak,nonatomic) UITextField *searchTextField;

@property(strong,nonatomic) UIButton *shuxxingBut;
@property(strong,nonatomic) UIImageView *zhuangtaiLanImageView;

@property(strong,nonatomic) NSMutableArray *kongzhiqiMuArr;
@property(strong,nonatomic) NSDictionary *dataDic;


@end

@implementation HWGameClassificationViewController






- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    navBarHairlineImageView.hidden = YES;
    
    
    
    //  self.navigationController.navigationBarHidden = YES;
    
    
    
    
    
    
}










//获取控制器视图

-(void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    //     navBarHairlineImageView.hidden = NO;
    
    //     self.navigationController.navigationBarHidden = NO;
    
    //我的收藏
    
}




- (SDCycleScrollViewCdb *)cycleScrollView {
    if (!_cycleScrollView) {
        // 网络加载 --- 创建不带标题的图片轮播器
        _cycleScrollView = [SDCycleScrollViewCdb cycleScrollViewWithFrame:CGRectMake(10, 5, kScreenW-2*10, 200) imageURLStringsGroup:nil];
        
        _cycleScrollView.infiniteLoop = YES;
        _cycleScrollView.delegate = self;
        _cycleScrollView.placeholderImage=[UIImage imageNamed:@"banner"];
        _cycleScrollView.pageControlStyle = SDCycleScrollViewCdbPageContolStyleNone;
        _cycleScrollView.autoScrollTimeInterval = 2.0; // 轮播时间间隔，默认1.0秒，可自定义
        _cycleScrollView.backgroundColor = [UIColor clearColor];
        
    }
    return _cycleScrollView;
}



- (NSMutableArray *)titleButtons
{
    if (_titleButtons == nil) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 设置常用的属性
    [self setproperties];
    // 1.添加标题滚动视图
    [self setupTitleScrollView];
    // 2.添加内容滚动视图
    [self setupContentScrollView];
    
    
    navBarHairlineImageView = [self findHairlineImageViewUnder:  self.navigationController.navigationBar];
    
    //    [self addshuxingkongjian];
    
    //3. 设置标题内容
    // self.biaotiArray = @[@"网游",@"BT版",@"折扣",@"H5游"];
    
    self.biaotiArray = [[NSMutableArray alloc]init];
    
    
    
    //    [self.biaotiArray addObject:@"分类"];
    //
    //    [self.biaotiArray addObject:@"热游榜"];
    //    [self.biaotiArray addObject:@"新游榜"];
    //    [self.biaotiArray addObject:@"预约榜"];
    //    [self.biaotiArray addObject:@"开服表"];
    
    
    [self.biaotiArray addObject:[HWjianzhiduiObject keylocalization:@"分类"]];
    [self.biaotiArray addObject:[HWjianzhiduiObject keylocalization:@"热游榜"]];
    [self.biaotiArray addObject:[HWjianzhiduiObject keylocalization:@"新游榜"]];
    [self.biaotiArray addObject:[HWjianzhiduiObject keylocalization:@"预约榜"]];
    [self.biaotiArray addObject:[HWjianzhiduiObject keylocalization:@"开服表"]];
    
    
    //  self.biaotiArray = @[@"网游",@"BT版"];
    //发送网络请求
    [self networkRequestData];
    
    //    //4.设置子控制器
    //    [self setupAllChildViewController];
    //
    //    // 5.设置所有标题
    //    [self setupAllTitle];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    UIView *fengexianview = [[UIView alloc]initWithFrame:CGRectMake(0, CF_NavHeight+43, kScreenW, 1)];
    fengexianview.backgroundColor =  RGB(236, 236, 236);
    [self.view addSubview:fengexianview];
    
    
    
    
    
    
    
    
}














- (void)addshuxingkongjian {
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"刷新" forState:UIControlStateNormal];
    button.frame = CGRectMake((kScreenW-120)/2, kScreenH/2-24, 120, 40);
    [button setTitleColor:RGB(255, 255 , 255) forState:UIControlStateNormal];
    self.shuxxingBut = button;
    self.shuxxingBut.backgroundColor = RGB(217, 247, 250);
    button.layer.cornerRadius = 20;
    button.layer.masksToBounds = YES;
    
    [button  addTarget:self action:@selector(shuxingTap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)shuxingTap {
    
    [self networkRequestData];
    
    
}





- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    
    return nil;
    
}



- (void)networkRequestData {
    
    
    //4.设置子控制器
    [self setupAllChildViewController];
    // 5.设置所有标题
    [self setupAllTitle];
    
    
    
    return ;
//    [SVProgressHUD show];
//    __weak typeof(self) weakSelf = self;
//    self.biaotiArray = [[NSMutableArray alloc]init];
//    self.kongzhiqiMuArr = [[NSMutableArray alloc]init];
//    NSDictionary *parametes = @{
//        @"ct" :  @"app",
//        @"ios" : @"1",
//        @"ac" : @"config",
//        @"member_id":[HWLocallyStoredData getCode],
//        
//    };
//    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
//        
//        [SVProgressHUD dismiss];
//        
//        
//        
//        
//        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//        if ([codeStr intValue] == 1) {
//            
//            self.shuxxingBut.hidden = YES;
//            
//            NSString *configStr = [NSString stringWithFormat:@"%@",response[@"data"][@"config"]];
//            
//            
//            
//            NSArray *arr = response[@"data"][@"index_top"];
//            for (int i = 0; i < arr.count; i++) {
//                
//                if ([arr[i][@"status"]  intValue]  == 1){
//                    
//                    [self.biaotiArray addObject:arr[i][@"name"]];
//                    [self.kongzhiqiMuArr addObject:arr[i]];
//                    
//                }
//                
//                
//            }
//            
//            //4.设置子控制器
//            [self setupAllChildViewController];
//            // 5.设置所有标题
//            [self setupAllTitle];
//            
//            
//            return;
//            
//            if ([configStr isEqualToString:@"65415"]) {
//                
//                NSArray *arr = response[@"data"][@"index_top"];
//                for (int i = 0; i < arr.count; i++) {
//                    
//                    if ([arr[i][@"status"]  intValue]  == 1){
//                        
//                        [self.biaotiArray addObject:arr[i][@"name"]];
//                        [self.kongzhiqiMuArr addObject:arr[i]];
//                    }
//                    
//                    
//                }
//                
//                //4.设置子控制器
//                [self setupAllChildViewController];
//                // 5.设置所有标题
//                [self setupAllTitle];
//                
//                
//                
//                
//            } else if ([configStr isEqualToString:@"31561"]) {
//                
//                
//                NSArray *arr = response[@"data"][@"index_top"];
//                for (int i = 0; i < arr.count; i++) {
//                    
//                    if ([arr[i][@"status"]  intValue]  == 1){
//                        
//                        [self.biaotiArray addObject:arr[i][@"name"]];
//                        [self.kongzhiqiMuArr addObject:arr[i]];
//                        
//                    }
//                    
//                }
//                
//                //4.设置子控制器
//                [self setupAllChildViewController];
//                // 5.设置所有标题
//                [self setupAllTitle];
//                
//                
//            } else if ([configStr isEqualToString:@"98751"]) {
//                
//                
//                NSArray *arr = response[@"data"][@"index_top"];
//                for (int i = 0; i < arr.count; i++) {
//                    
//                    if ([arr[i][@"status"]  intValue]  == 1) {
//                        
//                        [self.biaotiArray addObject:arr[i][@"name"]];
//                        [self.kongzhiqiMuArr addObject:arr[i]];
//                    }
//                    
//                }
//                
//                //4.设置子控制器
//                [self setupAllChildViewController];
//                // 5.设置所有标题
//                [self setupAllTitle];
//                
//                
//            }
//            
//        }
//        
//        NSLog(@"%@",response);
//        
//        
//        
//    } failure:^(NSString *error) {
//        [SVProgressHUD dismiss];
//        
//        
//    }];
    
    
    
}



#pragma mark ---------- UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    // [self popUpSearchView];
    
    return NO;
}

- (void)popUpSearchView {
    
    
    HWSearchController *modal = [[HWSearchController alloc]init];
    modal.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:modal animated:YES completion:nil];
    
    //把当前控制器作为背景
    //  self.definesPresentationContext = YES;
    
    //设置模态视图弹出样式
    //    modal.modalPresentationStyle = UIModalPresentationOverFullScreen;
    //
    ////    CATransition * transition = [CATransition animation];
    ////
    ////    //设置动画类型（这个是字符串，可以搜索一些更好看的类型）
    ////    transition.type = @"moveOut";
    ////
    ////    //动画出现类型
    ////    transition.subtype = @"fromCenter";
    ////
    ////    //动画时间
    ////    transition.duration = 0.3;
    ////
    ////    //移除当前window的layer层的动画
    ////    [self.view.window.layer removeAllAnimations];
    ////    //将定制好的动画添加到当前控制器window的layer层
    ////    [self.view.window.layer addAnimation:transition forKey:nil];
    ////
    //    [self presentViewController:modal animated:YES completion:nil];
    
    
}




#pragma mark - 设置所有标题
- (void)setupAllTitle
{
    // 已经把内容展示上去 -> 展示的效果是否是我们想要的(调整细节)
    // 1.标题颜色 为黑色
    // 2.需要让titleScrollView可以滚动
    
    // 添加所有标题按钮
    NSInteger count = self.biaotiArray.count;
    CGFloat btnW = 70;
    CGFloat btnH = self.titleScrollView.bounds.size.height;
    CGFloat btnX = 0;
    for (NSInteger i = 0; i < count; i++) {
        
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        //  UIViewController *vc = self.childViewControllers[i];
        //   titleButton.backgroundColor = [UIColor redColor];
        
        //   titleButton.backgroundColor = [UIColor redColor];
        
        
        
        [titleButton setTitle:self.biaotiArray[i] forState:UIControlStateNormal];
        CGFloat  jiaju =  (kScreenW - count *70) / (count+ 1);
        
        jiaju =  5;
        
        
        
        btnX = i * btnW + jiaju*(i+1);
        
        titleButton.frame = CGRectMake(btnX, 0, btnW, btnH);
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //    titleButton.backgroundColor = [UIColor redColor];
        titleButton.titleLabel.font = [UIFont systemFontOfSize:14];
        // 监听按钮点击
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        // 把标题按钮保存到对应的数组
        [self.titleButtons addObject:titleButton];
        
        
        
        if (i == 0) {
            [self titleClick:titleButton];
            
            UIImageView *xiantiaoimageview = [[UIImageView alloc]initWithFrame:CGRectMake(btnX+10, btnH-2-6 -4-4, 50, 4)];
            
            xiantiaoimageview.image = [UIImage imageNamed:@""];
            //    xiantiaoimageview.backgroundColor = QCchunseshensetupian;
            
            xiantiaoimageview.backgroundColor = [UIColor bm_colorGradientChangeWithSizeView:xiantiaoimageview];
            
            xiantiaoimageview.layer.cornerRadius = 2;
            xiantiaoimageview.layer.masksToBounds = YES;
            self.xiantiaoimageview = xiantiaoimageview;
            
            [self.titleScrollView addSubview:xiantiaoimageview];
            
        }
        
        [self.titleScrollView addSubview:titleButton];
    }
    
    
    // 设置标题的滚动范围
    self.titleScrollView.contentSize = CGSizeMake(kScreenW, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    
    // 设置内容的滚动范围
    self.contentScrollView.contentSize = CGSizeMake(count * kScreenW, 0);
    
    // bug:代码跟我的一模一样,但是标题就是显示不出来
    // 内容往下移动,莫名其妙
}


#pragma mark - 处理标题点击
- (void)titleClick:(UIButton *)button
{
    NSInteger i = button.tag;
    
    // 1.标题颜色 变成 红色
    [self selButton:button];
    
    //    CGRectMake(btnX+25, btnH-2-6, 50, 2)
    
    
    // 2.把对应子控制器的view添加上去
    [self setupOneViewController:i];
    
    // 3.内容滚动视图滚动到对应的位置
    CGFloat x = i * [UIScreen mainScreen].bounds.size.width;
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
}

#pragma mark - 选中标题
- (void)selButton:(UIButton *)button
{
    [_selectButton setTitleColor:RGB(68, 68, 68) forState:UIControlStateNormal];
    _selectButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    
    
    [button setTitleColor:RGB(68, 68, 68) forState:UIControlStateNormal];
    
    [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    
    
    
    
    
    
    _selectButton = button;
    
    NSInteger i = button.tag;
    NSInteger count = self.biaotiArray.count;
    CGFloat  jiaju =  (kScreenW - count *100) / (count+ 1);
    jiaju = 5;
    CGFloat btnW = 70;
    CGFloat btnX = i * btnW + jiaju*(i+1);
    self.xiantiaoimageview.frame = CGRectMake(btnX+10, 44-2-6-4-4, 50, 4);
    
    
}


#pragma mark - 添加一个子控制器的View
- (void)setupOneViewController:(NSInteger)i
{
    
    UIViewController *vc = self.childViewControllers[i];
    if (vc.view.superview) {
        return;
    }
    CGFloat x = i * [UIScreen mainScreen].bounds.size.width;
    vc.view.frame = CGRectMake(x, 0,   kScreenW, self.contentScrollView.bounds.size.height);
    [self.contentScrollView addSubview:vc.view];
}







#pragma mark - 添加标题滚动视图
- (void)setupTitleScrollView
{
    // 创建titleScrollView
    UIScrollView *titleScrollView = [[UIScrollView alloc] init];
    CGFloat y = CF_NavHeight;
    
    titleScrollView.frame = CGRectMake(0, y, self.view.bounds.size.width, 44);
    // titleScrollView.backgroundColor = [UIColor redColor];
    // titleScrollView.backgroundColor = RGB(217, 247, 250);
    [self.view addSubview:titleScrollView];
    
    UIImageView *imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    imageview1.image = [UIImage imageNamed:@"header-44GaoDu"];
    imageview1.hidden = YES;
    [titleScrollView addSubview:imageview1];
    
    
    _titleScrollView = titleScrollView;
    
    
    
}

#pragma mark - 添加内容滚动视图
- (void)setupContentScrollView
{
    // 创建contentScrollView
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    contentScrollView.frame = CGRectMake(0, y, self.view.bounds.size.width, self.view.bounds.size.height - y-TabBarHeight);
    [self.view addSubview:contentScrollView];
    _contentScrollView = contentScrollView;
    
    // 设置contentScrollView的属性
    // 分页
    self.contentScrollView.pagingEnabled = YES;
    // 弹簧
    self.contentScrollView.bounces = NO;
    // 指示器
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    
    contentScrollView.backgroundColor = [UIColor whiteColor];
    
    // 设置代理.目的:监听内容滚动视图 什么时候滚动完成
    self.contentScrollView.delegate = self;
    
    
    
    
}


// 只要一滚动就需要字体渐变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 字体缩放 1.缩放比例 2.缩放哪两个按钮
    NSInteger leftI = scrollView.contentOffset.x / kScreenW;
    
    UIButton *leftBtn = self.titleButtons[leftI];
    
    // 1.标题颜色 变成 红色
    [self selButton:leftBtn];
    
    // 添加控制器
    [self setupOneViewController:leftI];
    
    
    
}


#pragma mark - 添加所有子控制器
- (void)setupAllChildViewController
{
    
    
    
    
    for (int i = 0 ; i < self.biaotiArray.count ; i++) {
        
        
        if (i == 0 ) {
            
            HWFenleishitTuViewController *felei = [[HWFenleishitTuViewController alloc]init];
            [self addChildViewController:felei];
            
            
        } else if (i == 1) {
            
            HWRenYoubangViewController  *ren = [[HWRenYoubangViewController alloc]init];
            
            [self addChildViewController:ren];
            
            
            
        } else if (i == 2) {
            
            
            HWxingyoubangViewController *xingyou = [[HWxingyoubangViewController alloc]init];
            [self addChildViewController:xingyou];
            
            
            
        } else if (i == 3) {
            
            
            HWyuyueBanViewController *yuyue = [[HWyuyueBanViewController alloc]init];
            
            [self addChildViewController:yuyue];
            
            
        }else if (i == 4) {
            
            HWQCKaiFViewController *kaifu =  [[HWQCKaiFViewController alloc]init];
            
            [self addChildViewController:kaifu];
            
            
            
        }
        
    }
    
}


- (void)setproperties {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *titleview1  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 44)];
    titleview1.backgroundColor = [UIColor clearColor];
    
    self.navigationItem.titleView = titleview1;
    
    
    
    
    
    UITextField *texfeild = [[UITextField alloc]initWithFrame:CGRectMake(10, 5, kScreenW-20-60, 34)];
    texfeild.delegate = self;
    texfeild.placeholder = @"快来搜索你喜欢的游戏";
    texfeild.font = [UIFont systemFontOfSize:14];
    texfeild.backgroundColor = RGB(244, 243, 252);
    
    texfeild.placeholder = [HWjianzhiduiObject keylocalization:@"快来搜索你喜欢的游戏"];
    
    UIView *sousuoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 32)];
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(5, 0, 30, 30);
    [button setImage:[UIImage imageNamed:@"feileidesousu"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"feileidesousu"] forState:UIControlStateHighlighted];
    [sousuoView addSubview:button];
    
    texfeild.leftView = sousuoView;
    
    texfeild.leftViewMode = UITextFieldViewModeAlways;
    texfeild.layer.cornerRadius = 10;
    texfeild.layer.masksToBounds = YES;
    //  texfeild.backgroundColor = [UIColor whiteColor];
    
    //这里
    //    self.navigationItem.titleView = texfeild;
    
    [titleview1 addSubview:texfeild];
    
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(popUpSearchView)];
    
    [texfeild addGestureRecognizer:tap];
    
    
    
    
    
    UIImageView *zhongdianiamge = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    zhongdianiamge.image = [UIImage imageNamed:@"xiaoxi_ico"];
    
    
    
    UIButton  *but  = [UIButton buttonWithType:UIButtonTypeCustom];
    but.frame = CGRectMake(kScreenW-20-50+20-5, 7, 30, 30);
    
    [but  setImage:[UIImage imageNamed:@"xiaoxi_ico"] forState:UIControlStateNormal];
    but.backgroundColor = [UIColor clearColor];
    //     [but addTarget:self action:@selector(popUpSearchView) forControlEvents:UIControlEventTouchUpInside];
    [titleview1  addSubview:but];
    
    
}



@end
